//
//  HomeWaterNowViewController.m
//  ioschishan
//
//  Created by 马田成 on 2017/8/1.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "HomeWaterNowViewController.h"

@interface HomeWaterNowViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *watherTF;
@property (weak, nonatomic) IBOutlet UITextField *numTF;
@property (weak, nonatomic) IBOutlet UILabel *watherLaber;

@end

@implementation HomeWaterNowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationView];
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)grantWatherButton:(UIButton *)sender {
    
    [self.numTF endEditing:YES];
    [self.watherTF endEditing:YES];

    if ([self.watherTF.text isEqualToString:@"0"]) {
        
         [PublicFunction showAlert:nil message:@"水滴不足" duration:10.0f];
  
    }else {

        [self.delegate getWater:self.watherTF.text andNum:self.numTF.text];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)creationView{

     [self setTitle:@"赏水滴"];
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];

    [[MNetworkHandler sharedInstance]conURL:home_MemberGetmydata networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
        
            self.watherLaber.text = [returnData objectForKey:@"data"];

        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
       
    }];
    self.watherTF.keyboardType = UIKeyboardTypeNumberPad;
    self.numTF.keyboardType = UIKeyboardTypeNumberPad;
    self.watherTF.delegate = self;
    self.numTF.delegate = self;
    self.numTF.enabled = NO;
//    if ([self.watherLaber.text isEqualToString:@"0"]) {
//        self.numTF.enabled = NO;
//        self.watherTF.enabled = NO;
//    }else {
//        self.numTF.enabled = YES;
//        self.watherTF.enabled = YES;
//    }123
    


}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    
    if ([self.watherTF.text intValue] > [self.watherLaber.text intValue]) {
        self.watherTF.enabled = NO;
        
       
        
        [PublicFunction showAlert:nil message:@"水滴不足" duration:10.0f];

        self.watherTF.text = self.watherLaber.text;
        
    }else {
        self.watherTF.enabled = YES;

    }
    if ([self.watherTF.text isEqualToString:@"0"]) {
        [self.numTF endEditing:YES];
        [self.watherTF endEditing:YES];
        self.numTF.enabled = NO;
        self.numTF.text = @"0";
    }else {
    
        self.numTF.enabled = YES;
        
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{


    if ([self.watherTF.text intValue] > [self.watherLaber.text intValue]) {
        self.watherTF.enabled = NO;
        
        
        
        [PublicFunction showAlert:nil message:@"水滴不足" duration:10.0f];
        
        self.watherTF.text = self.watherLaber.text;
        
    }else {
        self.watherTF.enabled = YES;
        
    }
    
    if ([self.watherTF.text isEqualToString:@"0"]) {
        
        [self.numTF endEditing:YES];
        [self.watherTF endEditing:YES];
        self.numTF.enabled = NO;
        self.numTF.text = @"0";
    }else {
        
        self.numTF.enabled = YES;
        
    }
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
